package configClient;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class MysqlExecuteHelper {

    /**
     * jdbc:mysql://10.16.158.90:3306/db1"
     */
    private static final String MYSQL_URL_TEMPLATE = "jdbc:mysql://%s:%s/%s";

    public static ResultSet executeQuery(String ip,
                                  String port,
                                  String username,
                                  String password,
                                  String database,
                                  String querySql) {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            String mysqlUrl = String.format(MYSQL_URL_TEMPLATE, ip, port, database);
            Connection com = DriverManager.getConnection(mysqlUrl, username, password);
            Statement stat = com.createStatement();
            return stat.executeQuery(querySql);
        } catch (Exception e) {
            return null;
        }
    }
}
